﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PluggableModulesInterface;
using StorageManagement;
using System.Threading;
using SMSROUTER.Business;
using Utils;
using PluggableModulesRouter;
using SMSROUTER.StorageManagement;
using Settings;

namespace StaticThreadManagement
{
    public class XmlReaderTheadManage : WorkingBaseTimer
    {
        protected override int DUETIME { get { return 60000; } }
        protected override int PERIOD { get { return 600000; } }

        public override void Start()
        {
            base.Start();
            Xml.XmlNotify -= new Xml.OnXmlNotify(Xml_XmlNotify);
            Xml.XmlNotify += new Xml.OnXmlNotify(Xml_XmlNotify);
        }

        public override void Stop()
        {
            base.Stop();
            Xml.XmlNotify -= new Xml.OnXmlNotify(Xml_XmlNotify);
        }

        protected void Xml_XmlNotify()
        {
            int sync = Interlocked.CompareExchange(ref syncPoint, 1, 0);
            if (sync == 0)
            {
                ChangeTimer(DUETIME, PERIOD);
            }
        }

        protected override void LoopWorking()
        {
            try
            {
                SmsOutWaitingStorage.EnqueueXmlData();
                SmsOutReportStorage.EnqueueXmlData();

                SmsInWaitingStorage.EnqueueXmlData();
                SmsInStatisticStorage.EnqueueXmlData();

                Log.EnqueueXmlData();

                SmsRouterReport.ReadFromXmlFile();
            }
            catch (Exception ex)
            {
                HandleException(ex, AppSetting.WarningType.application_error);
            }
        }
    }
}
